package org.jeecg.modules.nc.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.nc.entity.NcInoutbusiclass;
import org.jeecg.modules.nc.service.INcInoutbusiclassService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.nc.service.INcInoutuseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: NC收支项目
 * @Author: qixin-boot
 * @Date:   2022-02-16
 * @Version: V1.0
 */
@Api(tags="NC收支项目")
@RestController
@RequestMapping("/nc/ncInoutbusiclass")
@Slf4j
public class NcInoutbusiclassController extends JeecgController<NcInoutbusiclass, INcInoutbusiclassService> {
	@Autowired
	private INcInoutbusiclassService ncInoutbusiclassService;
	@Autowired
	private INcInoutuseService ncInoutuseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param ncInoutbusiclass
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "NC收支项目-分页列表查询")
	@ApiOperation(value="NC收支项目-分页列表查询", notes="NC收支项目-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(NcInoutbusiclass ncInoutbusiclass,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<NcInoutbusiclass> queryWrapper = QueryGenerator.initQueryWrapper(ncInoutbusiclass, req.getParameterMap());
		queryWrapper.eq("dr",0).eq("enablestate",2);
		Page<NcInoutbusiclass> page = new Page<NcInoutbusiclass>(pageNo, pageSize);
		IPage<NcInoutbusiclass> pageList = ncInoutbusiclassService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param ncInoutbusiclass
	 * @return
	 */
	@AutoLog(value = "NC收支项目-添加")
	@ApiOperation(value="NC收支项目-添加", notes="NC收支项目-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody NcInoutbusiclass ncInoutbusiclass) {
		ncInoutbusiclassService.save(ncInoutbusiclass);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param ncInoutbusiclass
	 * @return
	 */
	@AutoLog(value = "NC收支项目-编辑")
	@ApiOperation(value="NC收支项目-编辑", notes="NC收支项目-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody NcInoutbusiclass ncInoutbusiclass) {
		ncInoutbusiclassService.updateById(ncInoutbusiclass);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "NC收支项目-通过id删除")
	@ApiOperation(value="NC收支项目-通过id删除", notes="NC收支项目-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		ncInoutbusiclassService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "NC收支项目-批量删除")
	@ApiOperation(value="NC收支项目-批量删除", notes="NC收支项目-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ncInoutbusiclassService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "NC收支项目-通过id查询")
	@ApiOperation(value="NC收支项目-通过id查询", notes="NC收支项目-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		NcInoutbusiclass ncInoutbusiclass = ncInoutbusiclassService.getById(id);
		if(ncInoutbusiclass==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ncInoutbusiclass);
	}

	/**
	* @Description NC收支项目-通过组织id查询
	* @author  wujinbiao
	* @date 2022/2/16
	* @param
	* @return
	*/
	@ApiOperation(value="NC收支项目-通过组织id查询", notes="NC收支项目-通过组织id查询")
	@GetMapping(value = "/queryByOrgId")
	public Result<?> queryByOrgId(@RequestParam(name="id",required=true) String orgId) {
		List<NcInoutbusiclass> ncInoutbusiclassList = new ArrayList<>();

		// 获取nc_inoutbusiclass、nc_inoutuse两张表的收支项目集合
		List<String> ids = ncInoutbusiclassService.selectByOrgId(orgId);
		List<String> list = ncInoutuseService.selectByOrgId(orgId);
		list.removeAll(ids);
		list.addAll(ids);

		if(list == null && list.size() < 0){
			return Result.error("未找到对应数据");
		}

		for (String str:list) {
			NcInoutbusiclass ncInoutbusiclass = ncInoutbusiclassService.getById(str);
			if (ncInoutbusiclass != null){
				ncInoutbusiclassList.add(ncInoutbusiclass);
			}
		}
		// update-end-by wujinbiao 2022/2/26 12:28 for
		return Result.OK(ncInoutbusiclassList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ncInoutbusiclass
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NcInoutbusiclass ncInoutbusiclass) {
        return super.exportXls(request, ncInoutbusiclass, NcInoutbusiclass.class, "NC收支项目");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NcInoutbusiclass.class);
    }

}
